package com.chain.chl.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.os.Bundle;
import android.view.View;

import android.widget.TabHost;
import android.widget.Toast;

import com.chain.chl.R;
import com.chain.chl.manager.CardManager;
import com.chain.chl.model.AddFavoriteModel;
import com.chain.chl.model.BaseResponse;
import com.chain.chl.model.TabConfig;
import com.chain.chl.model.card.Card;
import com.chain.chl.record.FavouriteShare;
import com.chain.chl.record.ShareListModel;
import com.chain.chl.record.ShareModel;
import com.chain.chl.ui.fragment.MeFragment;
import com.chain.chl.ui.fragment.RecFragment;
import com.chain.chl.widget.Tab;
import com.google.gson.Gson;
import com.naschain.naspay.NasPayManager;
import com.zzti.fengyongge.imagepicker.PhotoSelectorActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    @BindView(R.id.tabhost)
    FragmentTabHost mTabHost;

    private List<Tab> tabList = new ArrayList<>();

    public static void open(Activity context) {
        context.startActivity(new Intent(context, MainActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        setupTabHost();

        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                for (Tab tab : tabList) {
                    if (tab.getTag().equals(tabId)) {
                        tab.setState(true);
                    } else {
                        tab.setState(false);
                    }
                }
            }
        });
    }

    private void setupTabHost() {
        //addTab(Tab.TabType.REC, SlideFragment.class);
        addTab(Tab.TabType.REC, RecFragment.class);
        addTab(Tab.TabType.ME, MeFragment.class);
    }

    private void addTab(@Tab.TabType String tabType, Class fragmentClass) {
        TabConfig config = new TabConfig(tabType);
        View view = createTabView(this, config);
        if (tabType.equals(Tab.TabType.ADD)) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, PhotoSelectorActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    intent.putExtra("limit", 9);//number是选择图片的数量
                    startActivityForResult(intent, 123);
                }
            });
        }
        mTabHost.addTab(mTabHost.newTabSpec(tabType).setIndicator(view), fragmentClass, null);
    }

    private View createTabView(Context context, TabConfig tabConfig) {
        Tab tab = new Tab(getBaseContext());
        tab.init(tabConfig);
        tabList.add(tab);
        //        ViewConfig viewConfig = new ViewConfig();
        //        viewConfig.tabConfig = tabConfig;
        //        ButterKnife.bind(viewConfig, view);
        //        viewConfig.imageView.setImageDrawable(viewConfig.tabConfig.getImageDrawable(context));
        //        viewConfig.tabName.setText(viewConfig.tabConfig.getTextString(context));
        //        tabList.add(viewConfig);
        return tab;
    }

    @Override
    protected void onResume() {
        super.onResume();
        ShareListModel shareListModel = FavouriteShare.getPendingPayRecord(this);
        for (ShareModel shareModel : shareListModel.getShareModelList()) {
            NasPayManager.queryNasPay(shareModel.getUnicode(), new NasPayManager.OnNasPayUtilListener() {
                @Override
                public void success(String unicode) {
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "购买成功", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    });
                    String extra = FavouriteShare.removePendingPayRecord(MainActivity.this, unicode);
                    Card card = new Gson().fromJson(extra,Card.class);
                    addFavouriteCard(card);
                    FavouriteShare.addAlreadyPayRecord(MainActivity.this, card.getUrl(), unicode);
                }

                @Override
                public void fail(String unicode, final String msg) {
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT)
                                    .show();
                        }
                    });

                    FavouriteShare.removePendingPayRecord(MainActivity.this, unicode);
                }
            });
        }
    }


    private void addFavouriteCard(Card card){
        AddFavoriteModel addFavoriteModel = new AddFavoriteModel();
        addFavoriteModel.setCardExtraContent(card);
        CardManager.getInstance().addFavouriteCard(addFavoriteModel)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BaseResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {

                    }
                });
    }

}
